//
//  LDFinderSearchResultVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/5/27.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//



import UIKit

enum LDSearchFilterType:String {
    
    case Salary =       "salary"
    case Experience =   "experience"
    case Size =         "size"
}

class LDFinderSearchResultVC: UIViewController {
    
    
    var btnSecStatus = false
    
    var divideLine1:UIView!
    var divideLine2:UIView!
    
    var titleBtn1:UIButton!
    var titleBtn2:UIButton!
    var titleBtn3:UIButton!
    
    var clickBtn:((btn:UIButton) -> Void)?
    var tableView:UITableView!
    var tableViewResultVC:LDJobFinderFoundVC!
    
    var jobsInfoArr = [NSDictionary]() {
        
        didSet{
            
            tableViewResultVC.jobsArr = jobsInfoArr
        }
    }
    
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        
        view.backgroundColor = LDGolbalColor.BgGoundGray
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        
        //setDefaultContent()//暂时屏蔽
        setupTableView()
//        touchEventBlock()//暂时屏蔽
        requestNet()
    }
    
    //
    func requestNet() {
        
        LDUserInfoNet.sharedInstance.searchJobs(page: 0,filterid: 0, filtertype: LDSearchFilterType.Salary.rawValue, success: {
            [weak self]
            (hasNextPage, jobsArr) in
            
            self?.jobsInfoArr = jobsArr
            
        }) { (data) in
            
        }
    }
    
    //
    func setupTableView() {
        
        tableViewResultVC = LDJobFinderFoundVC()
        addChildViewController(tableViewResultVC)
        tableView = tableViewResultVC.tableView
        tableView.separatorStyle = .None
        
        tableView.frame = view.frame//CGRectMake(0, 0, LDFrame.ScreenWidth, LDFrame.ScreenHeight)
        tableView.contentInset = UIEdgeInsetsMake(8, 0, 0, 0)
        view.addSubview(tableView)
        
    }
    
    
    //
    func touchEventBlock() {
        
        titleBtn1.bk_whenTapped { [weak self] in
            
            self?.btnSecStatus = !(self?.btnSecStatus)!
            self?.titleBtn1.selected = (self?.btnSecStatus)!
            
            self?.tapBtnEvent((self?.titleBtn1)!)
        }
        
        titleBtn2.bk_whenTapped { [weak self] in
            
            self?.btnSecStatus = !(self?.btnSecStatus)!
            self?.titleBtn1.selected = (self?.btnSecStatus)!
            
            self?.tapBtnEvent((self?.titleBtn2)!)
        }
        
        titleBtn3.bk_whenTapped { [weak self] in
            
            self?.btnSecStatus = !(self?.btnSecStatus)!
            self?.titleBtn1.selected = (self?.btnSecStatus)!
            self?.tapBtnEvent((self?.titleBtn3)!)
            
        }
    }
    
    //tap 事件
    func tapBtnEvent(btn:UIButton) {
        
        let manger = LDCheckPublicInfo.shareInstance
        
        let index = btn.tag
        
        switch index {
            
        case 0:
            
            var textArr = [String]()
            var textArrID = [String]()
            
            manger.checkoutAllSalary({ [weak self] (data) in
                
                for item in data {
                    
                    textArr.append(item["salary_text"] as? String ?? LDNull)
                    textArrID.append(item["salary_id"] as? String ?? LDNull)
                }
                
                self?.showLDActionSheetView(nil, labHeight: 35, labsTitleArr: textArr, selClosure: {
                    
                    [weak self]
                    (index) in
                    
                    self?.tableViewResultVC.searchFiterType = LDSearchFilterType.Salary.rawValue
                    self?.tableViewResultVC.searchFiterID = index + 1//textArrID[index]
                    })
                
                })
            
        case 1:
            
            var textArr = [String]()
            var textArrID = [String]()
            
            manger.checkoutAllExperience({ [weak self] (data) in
                
                for item in data {
                    
                    textArr.append(item["experience_text"] as? String ?? LDNull)
                    textArrID.append(item["experience_id"] as? String ?? LDNull)
                }
                
                self?.showLDActionSheetView(nil, labHeight: 35, labsTitleArr: textArr, selClosure: { (index) in
                    //
                    self?.tableViewResultVC.searchFiterType = LDSearchFilterType.Experience.rawValue
                    self?.tableViewResultVC.searchFiterID = index + 1
                    
                })
                
                })
            
            
        case 2:
            
            var textArr = [String]()
            var textArrID = [String]()
            
            manger.checkoutAllMainEduInfo({ [weak self] (data) in
                
                for item in data {
                    
                    textArr.append(item["edu_text"] as? String ?? LDNull)
                    textArrID.append(item["edu_id"] as? String ?? LDNull)
                }
                
                self?.showLDActionSheetView(nil, labHeight: 35, labsTitleArr: textArr, selClosure: { (index) in
                    //
                    self?.tableViewResultVC.searchFiterType = LDSearchFilterType.Size.rawValue
                    self?.tableViewResultVC.searchFiterID = index + 1
                    
                })
                
                })
            
        default:
            break
        }
    }
    
    func setDefaultContent() {
        
        
        divideLine1 = UIView()
        divideLine1.backgroundColor = LDIconColor.LightGray
        
        divideLine2 = UIView()
        divideLine2.backgroundColor = LDIconColor.LightGray
        
        
        titleBtn1 = UIButton.setDetailsBtn("月薪", picName: "灰小箭头-下", btnBackColor: LDFontColor.White, titleColor: LDFontColor.DeepGray)!
        titleBtn1.tag = 0
        titleBtn1.layer.cornerRadius = 0
        
        
        titleBtn2 = UIButton.setDetailsBtn("经验", picName: "灰小箭头-下", btnBackColor: LDFontColor.White, titleColor: LDFontColor.DeepGray)!
        titleBtn2.tag = 1
        titleBtn2.layer.cornerRadius = 0
        
        
        titleBtn3 = UIButton.setDetailsBtn("学历", picName: "灰小箭头-下", btnBackColor: LDFontColor.White, titleColor: LDFontColor.DeepGray)!
        titleBtn3.tag = 2
        titleBtn3.layer.cornerRadius = 0
        
        
        
        titleBtn1.setImage(UIImage.init(named: "灰小箭头-上"), forState: .Selected)
        titleBtn2.setImage(UIImage.init(named: "灰小箭头-上"), forState: .Selected)
        titleBtn3.setImage(UIImage.init(named: "灰小箭头-上"), forState: .Selected)
        
        
        let btn_W:CGFloat = LDFrame.ScreenWidth/3
        let btn_H:CGFloat = 33
        
        titleBtn1.frame = CGRectMake(0, 64, btn_W, btn_H)
        titleBtn2.frame = CGRectMake(btn_W, 64, btn_W, btn_H)
        titleBtn3.frame = CGRectMake(btn_W*2, 64, btn_W, btn_H)
        
        titleBtn1 = UIButton.setButtonImageToRight(titleBtn1)
        titleBtn2 = UIButton.setButtonImageToRight(titleBtn2)
        titleBtn3 = UIButton.setButtonImageToRight(titleBtn3)
        
        let lineBtnView = UIView()
        lineBtnView.backgroundColor = LDIconColor.LightGray
        
        
        view.addSubview(titleBtn1)
        view.addSubview(titleBtn2)
        view.addSubview(titleBtn3)
        view.addSubview(lineBtnView)
        
        view.addSubview(divideLine1)
        view.addSubview(divideLine2)
        
        
        lineBtnView.snp_makeConstraints { (make) in
            
            make.top.equalTo(titleBtn1.snp_bottom).offset(-1)
            make.size.equalTo(CGSizeMake(LDFrame.ScreenWidth, 1))
        }
        
        divideLine1.snp_makeConstraints { (make) in
            
            make.centerY.equalTo(titleBtn1)
            make.left.equalTo(titleBtn2)
            make.width.equalTo(0.5)
            make.height.equalTo(btn_H - 10)
        }
        
        divideLine2.snp_makeConstraints { (make) in
            
            make.top.bottom.width.height.equalTo(divideLine1)
            make.right.equalTo(titleBtn2)
        }
    }
    
}

